<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
    public $username;
    public function __construct(){
        parent::__construct();
        $this->load->library('session');
    }
	public function login(){
		$this->load->helper(array('form', 'url'));
	    $this->load->library('form_validation');
	    $this->form_validation->set_error_delimiters('<span class="error help-block">', '</span>');
	    $this->form_validation->set_rules('username', '用户名', 'required|min_length[2]|max_length[10]|alpha_numeric');
		$this->form_validation->set_rules('password', '密码', 'required|alpha_dash|min_length[4]|max_length[32]|callback__auth_check');
	    if ($this->form_validation->run() == FALSE){
	        $this->load->view('admin/login');
	    }else{
            $newdata = array(
                'username' =>$this->username,
                'is_login' => TRUE
            );
            $this->session->set_userdata($newdata);
            redirect('/admin/Article/index');
	    }
    }
    public function _auth_check(){
    	$username=$this->input->post('username');
        $this->username=$username;
    	$password=md5($this->input->post('password'));
    	$this->load->model('admin_model');
    	$data=$this->admin_model->get_one($username,$password);
        if ($data){
            return true;
        }else{
            $this->form_validation->set_message('_auth_check', '用户名或密码不正确.');
            return FALSE;
        }
    }
    public function destroy(){
        $this->session->sess_destroy();
        redirect('/admin/Auth/login');
    }
}
